package com.uam.web.common.utils;



import com.alibaba.fastjson.JSONArray;
import com.uam.web.common.vo.TrackPoint;
import lombok.extern.slf4j.Slf4j;

import java.awt.geom.Point2D;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author: heguanglong
 * @date: 2020-12-22
 **/
@Slf4j
public class RegionUtil {
    private static double EARTH_RADIUS = 6378137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 计算两点间的距离 -米
     *
     * @param start 经纬度
     * @return
     */
    public static double getDistance( double[] start, double[] end) {

        double radLat1 = rad(start[0]);
        double radLat2 = rad(end[0]);
        double a = radLat1 - radLat2;
        double b = rad(start[1]) - rad(end[1]);
        double s = 2 * Math.asin(
                Math.sqrt(
                        Math.pow(
                                Math.sin(a / 2), 2
                        )

                                + Math.cos(radLat1) * Math.cos(radLat2)
                                *
                                Math.pow(
                                        Math.sin(b / 2), 2
                                )
                )
        );
        //弧长乘地球半径（半径为米）
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        DecimalFormat df = new DecimalFormat("#.000");
        return Double.valueOf(df.format(s));
    }


    /**
     * 求两个将经纬度的直线距离,单位M
     *
     * @param startLon 开始点经度
     * @param startLat 开始点纬度
     * @param endLon   结束点经度
     * @param endLat   结束点纬度
     * @return
     */
    public static double computeDistance(double startLon, double startLat, double endLon, double endLat) {
        double tempStartLat = startLat * Math.PI / 180.00D;
        double tempEndLat = endLat * Math.PI / 180.00D;
        return Math.round(2.00D
                * Math.asin(Math.sqrt(Math.pow(Math.sin((tempStartLat - tempEndLat) / 2.00D), 2.00D)
                + Math.cos(tempStartLat) * Math.cos(tempEndLat) * Math.pow(
                Math.sin((startLon * Math.PI / 180.00D - endLon * Math.PI / 180.00D) / 2.00D), 2.00D)))
                * 6378137.00D * 10000.00D) / 10000.00D;
    }

}
